package com.cwnu.onlinemall.controller;
import com.cwnu.onlinemall.pojo.MallCategory;
import com.cwnu.onlinemall.pojo.MallProduct;
import com.cwnu.onlinemall.pojo.MallSalesrecord;
import com.cwnu.onlinemall.pojo.ServerResult;
import com.cwnu.onlinemall.service.CategoryService;
import com.cwnu.onlinemall.service.CategoryServiceImpl;
import com.cwnu.onlinemall.service.ProductService;
import com.cwnu.onlinemall.service.SalesrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class ProductController {
    @Value("${server.port}")
    String port;//用来保存application.yml中的server.port的值



    @Autowired
    ProductService productService;

    @RequestMapping("/product/selectById")
    public ServerResult selectById(Integer id){
        MallProduct mallProduct = productService.selectById(id);
        return new ServerResult(0,"成功",mallProduct);


    }
    @RequestMapping("/product/insert")
    public ServerResult insert(MallProduct mallProduct){
        Integer insertRow = productService.insert(mallProduct);
        if (insertRow>=1){
            return new ServerResult(0,"成功",null);
        }else {
            return new ServerResult(1,"失败",null);
        }


    }
    @RequestMapping("/product/selectAll")
    public ServerResult selectAll(){
        List<MallProduct> mallProducts = productService.selectProductAll();
        return new ServerResult(0,"查询成功"+port,mallProducts);
    }

    @RequestMapping("/product/delectById")
    public ServerResult delectById(Integer id){
        Integer integer = productService.deleteById(id);
        if (integer>=1){
            return new ServerResult(0,"删除成功",null);

        }
        return new ServerResult(1,"删除失败",null);
    }

    @RequestMapping("/product/updateById")
    public ServerResult updateById(MallProduct mallProduct){
        Integer updateRow = productService.updateById(mallProduct);
        if (updateRow>=1){
            return new ServerResult(0,"修改成功",null);

        }
        return new ServerResult(1,"修改失败",null);
    }

}